package cn.hh.basic.controller;

import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/dfs")
public class FastDfsController {

    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        //获取文件名字
        String originalFilename = file.getOriginalFilename();
        //获取文件的后缀名
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        try {
            String   upload = FastDfsUtil.upload(file.getBytes(), extName);
            System.out.println("图片上传路径"+upload);
            return  AjaxResult.me().setResultObj(upload);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 参数：完整路径 /goup1/xxxxx/yyyy
     * 返回值：成功与否，还要返回地址
     *
     */
    @DeleteMapping
    public AjaxResult del(@RequestParam(required = true,value = "path") String path){
        String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
        System.out.println(groupName);
        System.out.println(remotePath);
        FastDfsUtil.delete(groupName, remotePath);
        return  AjaxResult.me();
    }
}
